package cn.io0288.blog2.db;

import cn.io0288.blog2.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class Classify {
    public static Map<Integer, String> classify;
    static {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        classify = new HashMap<Integer, String>();

        connection = JdbcUtils.getConnection();
        String sql = "SELECT * FROM classify ORDER BY cid";

        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                classify.put(Integer.valueOf(resultSet.getString("cid")), resultSet.getString("cname"));
            }

            System.out.println(classify.toString());
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static Map<Integer, String> getAllClassify() {
        return classify;
    }
    public static String getClassify(int cid) {
        return classify.get(cid);
    }
}





